package com.turing.post.antimoney.impl;

import java.math.BigInteger;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;

import com.turing.post.antimoney.util.AlarmInfo;
import com.turing.post.antimoney.util.AlarmInfoDao;
import com.turing.post.antimoney.util.AlarmInfoDetail;
import com.turing.post.antimoney.util.AlarmInfoDetailDao;
import com.turing.post.antimoney.util.AlarmRule;
import com.turing.post.antimoney.util.AlarmRulesBusiness;
import com.turing.post.common.util.PCPSCommonDao;
import com.turing.post.organization.util.SaleBranch;
import com.turing.post.organization.util.SaleBranchDao;
import com.turing.post.salesmanage.util.OrganizeCustom;
import com.turing.post.salesmanage.util.OrganizeCustomDao;
import com.turing.post.salesmanage.util.PersonalCustom;
import com.turing.post.salesmanage.util.PersonalCustomDao;
import com.turing.post.salesmanage.util.SaleOrder;
import com.turing.post.salesmanage.util.SaleOrderDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;

/**
 *  同一经办人在短时期内进行多次购买的未超过限额
 * 
 * @author 李治
 * 
 */
@Remote
@Local
@Stateless(mappedName = "antimoney.impl.AlarmRule16")
public class AlarmRule16 implements AlarmRulesBusiness {
	@EJB(mappedName = "salesmanage.impl.OrganizeCustomDaoImpl")
	private OrganizeCustomDao organizeCustomDaoImpl;
	@EJB(mappedName = "salesmanage.impl.SaleOrderDaoImpl")
	private SaleOrderDao saleOrderDaoImpl;
	@EJB(mappedName = "	salesmanage.impl.PersonalCustomDaoImpl")
	private PersonalCustomDao personalCustomDaoImpl;
	@EJB(mappedName = "common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao commonDaoImpl;
	@EJB(mappedName = "organization.impl.SaleBranchDaoImpl")
	private SaleBranchDao saleBranchDaoImpl;
	@EJB(mappedName = "antimoney.impl.AlarmInfoDaoImpl")
	private AlarmInfoDao alarmInfoDaoImpl;
	@EJB(mappedName = "antimoney.impl.AlarmInfoDetailDaoImpl")
	private AlarmInfoDetailDao alarmInfoDetailDaoImpl;
	
	//同一经办人在短时期内进行多次购买的未超过限额
	@Override
	@TransactionAttribute(javax.ejb.TransactionAttributeType.REQUIRES_NEW)
	public void runAlarmRules(String issueCompanyNo, Date tranDate,
			AlarmRule alarmRule) throws PCPSApplicationException {
			//1.设置参数
			String ruleParameter="";//规则
			int ruleday=0;//规则天数
			int rulenum=0;//规则笔数
			double rulemoney=0;//规则金额
			String buyerId="";//购卡人id
			String buyername="";//购卡姓名
			List<SaleOrder>	saleorderlist=null;
			try{
				if(!PCPSUtil.isEmpty(issueCompanyNo) || tranDate!=null || alarmRule!=null){//得到规则参数
					ruleParameter=alarmRule.getRuleParameter();			
				}
				if(!PCPSUtil.isEmpty(ruleParameter)){//给定义的参数赋值
					String[] parameter=ruleParameter.split(":");
					if(parameter.length>0){
						ruleday=parameter[0]==null?0:Integer.parseInt(parameter[0]);
						rulenum=parameter[1]==null?0:Integer.parseInt(parameter[1]);
						rulemoney=parameter[2]==null? 0 :Double.parseDouble(parameter[2]);
					}
				}
				Date startTime =getDateAddDay(tranDate,ruleday);//起始日期			
				//2.查询存在风险的购卡人
				List<Object[]> list = saleOrderDaoImpl.getSaleOrderMoreTimes(issueCompanyNo,startTime, tranDate, rulemoney, rulenum);
				if(list!=null && list.size() > 0){
					for(int i=0;i<list.size();i++){
						Object[] obj=list.get(i);
						if(obj==null || obj.length <= 0)
							continue;
						//得到购卡主题姓名
						if(obj[0] !=null){
							buyername=getPersonName(obj[0].toString());
							buyerId=obj[0].toString();
						}
						if(obj[1] !=null){
							buyername=getOrganizeCustom(obj[1].toString());
							buyerId=obj[1].toString();
						}					
						if(PCPSUtil.isEmpty(buyerId))
							continue;
						//根据参数得到销售单清单					
						saleorderlist=saleOrderDaoImpl.getSaleOrderByDateAndPer(issueCompanyNo, startTime, tranDate, buyerId);
						
						if(saleorderlist==null || saleorderlist.size() <= 0)
							continue;
						releaseAlarm(saleorderlist, issueCompanyNo, alarmRule, tranDate,
								buyername);// 发布风险信息
					}
				}
				
			}catch(Exception e){
				e.printStackTrace();
			}
	}
	/**
	 * 发布风险信息
	 * 
	 * @param tranDate
	 * @param alarmRule
	 * @param issueCompany
	 * 
	 * @param alarmOrders存在风险的销售单
	 * @throws PCPSApplicationException
	 */
	private void releaseAlarm(List<SaleOrder> alarmOrders,
			String issueCompanyno, AlarmRule alarmRule, Date tranDate,
			String personname) throws PCPSApplicationException {
		AlarmInfo alarmInfo = new AlarmInfo();
		SaleOrder lastSaleOrder = alarmOrders.get(0);
		SaleBranch saleBranch = saleBranchDaoImpl
				.getSaleBranchBySaleBranchNo(lastSaleOrder.getSaleBranchNo());
		alarmInfo.setAlarmRuleId(alarmRule.getAlarmRuleId());
		alarmInfo.setIssueCompanyNo(issueCompanyno);
		alarmInfo.setBranchNo(lastSaleOrder.getSaleBranchNo());
		alarmInfo.setBranchName(saleBranch.getSaleBranchName());
		alarmInfo.setAlarmTime(commonDaoImpl.getDatabaseSysTime());
		alarmInfo.setRiskCustomName(personname);
		alarmInfo.setAlarmReason(alarmRule.getAlarmRuleDesc());
		alarmInfo.setDealStatus("0");

		BigInteger alarmInfoId = alarmInfoDaoImpl.getMaxAlarmInfoId();
		alarmInfo.setAlarmInfoId(alarmInfoId);
		// 插入预警信息
		alarmInfoDaoImpl.insertAlarmInfo(alarmInfo);
		for (int i = 0; i < alarmOrders.size(); i++) {
			SaleOrder order = alarmOrders.get(i);
			AlarmInfoDetail alarmInfoDetail = new AlarmInfoDetail();
			alarmInfoDetail.setAlarmInfoId(alarmInfoId);
			alarmInfoDetail.setSeqNo(i);
			alarmInfoDetail.setBusinessType("0");
			alarmInfoDetail.setBusinessOrderId(order.getSaleOrderId());
			alarmInfoDetail.setTranDate(tranDate);
			alarmInfoDetailDaoImpl.insertAlarmInfoDetail(alarmInfoDetail);
		}
	}
	/**
	 * 时间减天数得到时间
	 * 
	 * @param time
	 * @param daynum
	 * @return
	 */

	@SuppressWarnings("unused")
	private Date getDateAddDay(Date tranTime, int ruleDay) {
		Calendar cal = java.util.Calendar.getInstance();
		cal.setTime(tranTime);
		cal.add(Calendar.DATE, -ruleDay);
		tranTime = cal.getTime();
		return tranTime;
	}
	private String getPersonName(String personid) {
		if (personid == null || "".equals(personid)) {
			return null;
		}
		PersonalCustom person = personalCustomDaoImpl
					.getPersonalCustomByPersonalId(new BigInteger(personid));
		if (person == null) {
			return null;
		}else		
		    return person.getName();
	}	

	private String getOrganizeCustom(String orgid) {
		if (orgid == null || "".equals(orgid)) {
			return null;
		}
		OrganizeCustom organizeCustom = organizeCustomDaoImpl
				.getOrganizeCustomByOrganizeId(new BigInteger(orgid));
		if (organizeCustom == null) {
			return null;
		}else		
		    return organizeCustom.getFullName();
	}

}
